
package action;

import beans.UserBean;
import db.Forum;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

/**
 * Action delete one forum.
 * @author Tomas CASTA
 */
@UrlBinding("/forumDelete.action")
public class DeleteForumActionBean implements ActionBean {
    /** action context */
    private ActionBeanContext context;
    /** forum */
    private Forum forum;

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    /**
     * Delete forum.
     * @return Resolution
     */
    public Resolution delete() {
        /* User is logged */
        if (null != context.getRequest().getSession().getAttribute("user")) {

            /* Get user info from session */
            UserBean user = (UserBean) context.getRequest().getSession().getAttribute("user");
            
            /* User is admin */
            if (user.getAdmin() == 1) {
                forum = new Forum();
                forum.setDb(this.context.getServletContext());
                
                /* Parse forum id from URI */
                try {
                    int id = Integer.valueOf(context.getRequest().getParameter("id"));

                    /* Delete forum */
                    forum.deleteForum(id);
                    return new ForwardResolution("/index.action");
                } catch (NumberFormatException e) {
                    Logger.getLogger(DeleteMessageActionBean.class.getName()).log(Level.SEVERE, null, e);
                    return new ForwardResolution("/index.action");
                }
            }
        }
        /* Some error, show index */
        return new ForwardResolution("/index.action");
    }
}
